﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WITM.DAL
{
    public class SpendingEntryRepository : ISpendingEntryRepository
    {
        private WhereIsTheMoneyDBEntities context;

        public SpendingEntryRepository(WhereIsTheMoneyDBEntities dbcontext)
        {
            context = dbcontext;
        }

        #region ISpendingEntryRepository Members

        public IEnumerable<SpendingEntries> GetAll()
        {
            return context.SpendingEntries.ToList();
        }

        public IEnumerable<SpendingEntries> Find(System.Linq.Expressions.Expression<Func<SpendingEntries, bool>> where)
        {
            return context.SpendingEntries.Include("SpendingItems").Where(where).ToList();
        }

        public SpendingEntries First(System.Linq.Expressions.Expression<Func<SpendingEntries, bool>> where)
        {
            return context.SpendingEntries.FirstOrDefault(where);
        }

        public void Add(SpendingEntries entry)
        {
            context.AddToSpendingEntries(entry);
        }

        public void Delete(SpendingEntries entry)
        {
            throw new NotImplementedException();
        }

        public void Attach(SpendingEntries entry)
        {
            context.Attach(entry);
        }

        public List<SpendingSum> GetTotalCategories(int userID, DateTime? startDate, DateTime? endDate)
        {
            var res = from e in context.SpendingEntries
                      where e.SpendingItems.SpendingCategories.Users.UserID == userID 
                      && ((startDate == null && endDate == null) || (e.Date >= startDate && e.Date <= endDate))
                      group e by e.SpendingItems.SpendingCategories into groupedCategories
                      select new SpendingSum
                      {
                          ID = groupedCategories.Key.CategoryID,
                          Description = groupedCategories.Key.Description,
                          Amount = groupedCategories.Sum(g => g.Price)
                      };
            return res.OrderByDescending(k => k.Amount).ToList();
        }

        public List<SpendingSum> GetTotalItems(int userID, DateTime? startDate, DateTime? endDate)
        {
            var res = from e in context.SpendingEntries
                      where e.SpendingItems.SpendingCategories.Users.UserID == userID
                      && ((startDate == null && endDate == null) || (e.Date >= startDate && e.Date <= endDate))
                      group e by e.SpendingItems into groupedItems
                      select new SpendingSum
                      {
                          ID = groupedItems.Key.ItemID,
                          Description = groupedItems.Key.Description,
                          Amount = groupedItems.Sum(g => g.Price)
                      };
            return res.OrderByDescending(k => k.Amount).ToList();
        }

        public List<SpendingSum> GetTotalDays(int userID, DateTime? startDate, DateTime? endDate)
        {
            var res = from e in context.SpendingEntries
                      where e.SpendingItems.SpendingCategories.Users.UserID == userID
                      && ((startDate == null && endDate == null) || (e.Date >= startDate && e.Date <= endDate))
                      group e by e.Date into groupedItems
                      select new SpendingSum
                      {
                          ID = 0,
                          Description = "",
                          Date = groupedItems.Key,
                          Amount = groupedItems.Sum(g => g.Price)
                      };
            return res.OrderByDescending(k => k.Date).ToList();
        }


        public decimal GetTotalPeriod(int userID, DateTime? startDate, DateTime? endDate)
        {
            throw new NotImplementedException("not implemented");
        }

        #endregion
    }
}
